/**
 * 
 */
package com.mc.yingshi;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * 根据appKey和secret获取accessToken
 * @author w.weizhen
 *
 */
public class Token {
	/**
     * @param args
     */
    public static void main(String[] args) {
        
    }
    private static String url = "https://open.ys7.com/api/lapp/token/get";
    private static String appKey="7b810bbbd6974827b127cc32336b555c";
    private static String appSecret="5f1f685fb87ff4355b089310a1167858";
    public static String getToken(String key,String secret)
    {
        if (StringUtils.isBlank(key)){key=appKey;}
        if (StringUtils.isBlank(secret)){secret=appSecret;}
        
        String accessToken = "";
        OkHttpClient okHttpClient = new OkHttpClient();
        RequestBody body = new FormBody.Builder()
            .add("appKey", key)
            .add("appSecret", secret)
            .build();
        Request request = new Request.Builder()
            .url(url)
            .post(body)
            .build();
        Call call = okHttpClient.newCall(request);
        try {
            Response response = call.execute();
            accessToken = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        return accessToken;
    }
}
